home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / OOPS / OBJECTA / OBJECTA.PAS < prev   
Pascal/Delphi Source File  |  1989-07-07  |  4KB  |  143 lines

  1. {$S-}
  2. UNIT ObjectA;
  3.  
  4. {
  5.             Rob Rosenberger        CIS: 74017,1344
  6.             Barn Owl Software      VOX: (618) 632-7345
  7.             P.O. Box #74           BBS: (618) 398-5703
  8.             O'Fallon, IL  62269    HST: (618) 398-2305
  9.  
  10.    This Turbo Pascal 5.5 unit builds on the OBJECTS.PAS file on disk three
  11. of the TP package.  The Objects unit provides some excellent linked-list
  12. capabilities, but it doesn't allow arbitrary placement of a node on the list.
  13. This unit offers a decendent, LinkList, which provides two extra methods:
  14. LinkList.Before, to place a node just before some other node in the list, and
  15. LinkList.After, to place a node just after some other node in the list.
  16.    LinkList.Init and LinkList.Done are included because the TP5.5 OOP guide
  17. recommends these common procedures.  The List ancestor calls them Clear and
  18. Delete, respectively, which I found extremely odd since _Borland_ is the one
  19. calling for a standard set of method identifiers....
  20.  
  21. Version 1.00: released to the public domain on 8 July 1989.
  22.    See above for the reason this was developed.
  23. }
  24.  
  25.  
  26. INTERFACE {section}
  27.  
  28.  
  29. USES
  30.    Objects;
  31.  
  32. TYPE
  33.    LinkList
  34.     = OBJECT(List)
  35.       PROCEDURE Init;
  36.       PROCEDURE Before(TheNode    : NodePtr;
  37.                        BeforeNode : NodePtr);
  38.       PROCEDURE After (TheNode   : NodePtr;
  39.                        AfterNode : NodePtr);
  40.       FUNCTION  Total(TheNode : NodePtr) : LONGINT;
  41.       PROCEDURE Done; VIRTUAL
  42.       END;
  43.  
  44.  
  45. IMPLEMENTATION {section}
  46.  
  47.  
  48. {============================================================================}
  49. PROCEDURE LinkList.Init;
  50.  
  51.    {This procedure initializes the LinkList.}
  52.  
  53. BEGIN {LinkList.Init}
  54. Clear
  55. END; {LinkList.Init}
  56. {============================================================================}
  57.  
  58. {============================================================================}
  59. PROCEDURE LinkList.Before(TheNode    : NodePtr;
  60.                           BeforeNode : NodePtr);
  61.  
  62.    {Places TheNode in the LinkList just before BeforeNode.}
  63.  
  64. BEGIN {LinkList.Before}
  65. IF (TheNode = NIL)
  66.  THEN
  67.     EXIT {there's nothing to add to our LinkList}
  68.  ELSE
  69.     IF ((BeforeNode = NIL)
  70.      OR (BeforeNode^.Prev = NIL))
  71.      THEN {append as first node on the LinkList}
  72.         Append(TheNode)
  73.      ELSE
  74.         BEGIN
  75.         TheNode^.Next          := BeforeNode;
  76.         BeforeNode^.Prev^.Next := TheNode
  77.         END
  78. END; {LinkList.Before}
  79. {============================================================================}
  80.  
  81. {============================================================================}
  82. PROCEDURE LinkList.After(TheNode   : NodePtr;
  83.                          AfterNode : NodePtr);
  84.  
  85.    {Places TheNode in the LinkList just after AfterNode.}
  86.  
  87. BEGIN {LinkList.After}
  88. IF (TheNode = NIL)
  89.  THEN
  90.     EXIT {there's nothing to add to our LinkList}
  91.  ELSE
  92.     IF ((AfterNode = NIL)
  93.      OR (AfterNode^.Next = NIL))
  94.      THEN {Insert as last node on the LinkList}
  95.         Insert(TheNode)
  96.      ELSE
  97.         BEGIN
  98.         TheNode^.Next   := AfterNode^.Next;
  99.         AfterNode^.Next := TheNode
  100.         END
  101. END; {LinkList.After}
  102. {============================================================================}
  103.  
  104. {============================================================================}
  105. FUNCTION  LinkList.Total(TheNode : NodePtr) : LONGINT;
  106.  
  107.    {This function returns a number corresponding to the number of nodes on the
  108. LinkList.  It starts counting from TheNode^.  Therefore, to get a total count
  109. of all nodes on the LinkList, use LinkListVar.Total(LinkListVar.First).}
  110.  
  111. VAR
  112.    TempLongint : LONGINT;
  113.  
  114. BEGIN {LinkList.Total}
  115. {Initialize.}
  116. TempLongint := 0;
  117.  
  118. IF (TheNode = NIL)
  119.  THEN
  120.     {Do nothing.}
  121.  ELSE
  122.     REPEAT
  123.         INC(TempLongint);
  124.         TheNode := TheNode^.Next
  125.      UNTIL (TheNode = NIL);
  126.  
  127. Total := TempLongint
  128. END; {LinkList.Total}
  129. {============================================================================}
  130.  
  131. {============================================================================}
  132. PROCEDURE LinkList.Done;
  133.  
  134.    {This procedure deletes all nodes from the list.}
  135.  
  136. BEGIN {LinkList.Done}
  137. Delete
  138. END; {LinkList.Done}
  139. {============================================================================}
  140.  
  141.  
  142. END. {ObjectA}
  143.